package com.fivefu.core.hik.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hik.entity.DbHikRegions;
import com.fivefu.core.hik.mapper.DbHikRegionsMapper;
import com.fivefu.core.hik.services.DbHikRegionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fivefu.module.dictionary.entity.DbSysDict;
import com.fivefu.module.dictionary.service.DbSysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 海康区域信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-06-30 14:07:20
 */
@Service
public class DbHikRegionsServiceImpl extends ServiceImpl<DbHikRegionsMapper, DbHikRegions> implements DbHikRegionsService {

    @Autowired
    private DbSysDictService dbSysDictService;
    /**
     * 获取未删除区域唯一标识集合
     * @return
     */
    @Override
    public List<String> queryRegionIDs(){
        QueryWrapper<DbHikRegions> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_status",0);
        wrapper.select("index_code");
        List<DbHikRegions> list = baseMapper.selectList(wrapper);
        List<String> regionIDs = list.stream().map(k -> k.getIndexCode()).collect(Collectors.toList());
        return regionIDs;
    }

    /**
     * 通过唯一标识查询区域信息
     * @param code
     * @return
     */
    public DbHikRegions queryHikRegionsByCode(String code){
        QueryWrapper<DbHikRegions> wrapper = new QueryWrapper<>();
        wrapper.eq("index_code", code);
        DbHikRegions regions = baseMapper.selectOne(wrapper);
        return regions;
    }


    /**
     * 编辑区域信息
     * @param regions
     */
    @Override
    public void editHikRegion(DbHikRegions regions) {
        String code = regions.getIndexCode();
        //通过唯一标识查询区域信息
        DbHikRegions oldRegions = queryHikRegionsByCode(code);
        if (oldRegions!=null){
            regions.setId(oldRegions.getId());
        }
        LocalDateTime time = LocalDateTime.now();
        regions.setUpdatedTime(time);

        //判断区域是否删除
        if (regions.getRegionStatus()!=null){
            if (regions.getRegionStatus()<0){
                regions.setIsDelete(1);
            }else {
                regions.setIsDelete(0);
            }
        }

        //将区域类型由海康提供的转换字典中保存的编号
        if (StrUtils.isNotNull(regions.getCatalogType())) {
            //根据dict_value查询监控点对应类型的dict_code
            DbSysDict dict = dbSysDictService.queryDictByValue("CT", regions.getCatalogType());
            if(dict != null){
                regions.setCatalogType(dict.getDictCode());
            }
        }
        if (regions.getId()!=null){
            baseMapper.updateById(regions);
        }else {
            if (regions.getRegionStatus() == null){
                regions.setRegionStatus(0);
            }
            regions.setCreatedTime(time);
            if (regions.getIsDelete()==null){
                regions.setIsDelete(0);
            }
            baseMapper.insert(regions);
        }
    }

    /**
     * 获取未删除区域的父区域编号
     * @return
     */
    @Override
    public List<String> queryRegionParentIds() {
        QueryWrapper<DbHikRegions> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_status",0);
        wrapper.select("distinct parent_index_code");
        List<DbHikRegions> list = baseMapper.selectList(wrapper);
        List<String> ParentIds = list.stream().map(k -> k.getParentIndexCode()).collect(Collectors.toList());
        return ParentIds;
    }

    /**
     * 查询海康区域所有数据
     * @return
     */
    @Override
    public List<DbHikRegions> queryRegionList() {
        QueryWrapper<DbHikRegions> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_status",0);
        List<DbHikRegions> list = baseMapper.selectList(wrapper);
        return list;
    }

    /**
     * 删除区域数据
     * @param regionCodeList     区域编号indexCode
     * @param userId
     */
    @Override
    public void deleteHikRegion(List<String> regionCodeList, String userId) {
            QueryWrapper<DbHikRegions> wrapper = new QueryWrapper<>();
            wrapper.eq("is_delete",0);
            if (!regionCodeList.isEmpty()) {
                wrapper.notIn("index_code",regionCodeList);
            }

            DbHikRegions hikRegions = new DbHikRegions();
            hikRegions.setIsDelete(1);
            hikRegions.setUpdatedTime(LocalDateTime.now());
            hikRegions.setUpdatedBy(userId);
            baseMapper.update(hikRegions,wrapper);
    }
}
